package org.mtmi.ui.actions;

import java.util.Collection;

/** default implementation for IRetargettableAction */
public class RetargetableAction extends Action implements IRetargetableAction
{
	protected Object target;
	
	public RetargetableAction( int type ) {
		this( type, null );
	}
	public RetargetableAction( int type, Object tgt ) {
		super( type );
		target=tgt;
	}
	
	@Override
	public void setTarget( Object c ) {
		target = c;
	}
	
	@Override
	public Object getTarget() {
		return target;
	}
	
	@Override /**@return false by default */
	public boolean isTargetHandled(Object c) {
		return false;
	}
	
	
	/** @return true if o is an instance of class, or a non empty collection containing element instance of class */
	public static boolean isInstanceOfOrCollectionOfInstanceOf( Object o, Class<?> clazz )
	{
		if( o==null ) return false;
		Class<?> c = o.getClass();
		if( clazz.isAssignableFrom( c ) ) return true;
		if( o instanceof Collection ) {
			Collection<?> l = (Collection<?>)o;
			for( Object e : l ) {
				if( e==null ) return false;
				Class<?> ec = e.getClass();
				if( !clazz.isAssignableFrom( ec ) ) return false; 
			}
			return l.size()>0;
		}
		return false;
	}
}
